# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# The jquery comboselect plugin: http://plugins.jquery.com/project/comboselect
# Jason Huck: http://devblog.jasonhuck.com/2008/04/25/jquery-combo-select-redux/
# Packaged as a tw.jquery widget by Ciaran Farrell <cfarrell1980@gmail.com>

from tw.api import Widget, JSLink, CSSLink, js_function, js_callback
from tw.forms.fields import MultipleSelectField
from tw.jquery.base import *

jQuery = js_function('$')

jquery_selso_js = JSLink(
        filename='static/javascript/jquery.selso.js',
        javascript=[jquery_js],
        modname=__name__)

jquery_comboselect_js = JSLink(
        filename='static/javascript/jquery.comboselect.js',
        javascript=[jquery_selso_js],
        modname=__name__)

jquery_comboselect_css = CSSLink(
        filename='static/css/jquery.comboselect.css',
        modname=__name__)

class ComboSelect(MultipleSelectField):
    javascript=[jquery_comboselect_js]
    css=[jquery_comboselect_css]
    def update_params(self,d):
        super(ComboSelect,self).update_params(d)
        self.update_attrs(d, "size")
        d['attrs']['multiple'] = True
        self.add_call("$(function(){$('#%s').comboselect({ sort: 'both', addbtn: '+',  rembtn: '-' });});"%d.id)

